package com.chandra.pos.employee.repository;

import com.chandra.pos.SearchCriteria;
import com.chandra.pos.employee.dao.entity.Employee;
import com.chandra.pos.employee.model.EmployeeSearchCriteria;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Admin
 * Date: 11/15/13
 * Time: 7:53 PM
 * To change this template use File | Settings | File Templates.
 */

public class EmployeeRepositoryImpl implements EmployeeRepositoryCustom {

    @PersistenceContext
    private EntityManager entityManager;


    @Override
    public List<Employee> search(EmployeeSearchCriteria searchCriteria) {


        CriteriaBuilder cb = entityManager.getCriteriaBuilder();
        CriteriaQuery<Employee> criteriaQuery=cb.createQuery(Employee.class) ;

        Root<Employee> userRoot = criteriaQuery.from(Employee.class);

        List<Predicate> predicates = new ArrayList<Predicate>();


        if (searchCriteria.getFirstName() != null)
        {

            predicates.add(cb.equal(userRoot.get("firstName"),searchCriteria.getFirstName()));

        }

        if (searchCriteria.getLastName() != null)
            predicates.add(cb.equal(userRoot.get("lastName"),searchCriteria.getLastName()));


        if (searchCriteria.getCity() != null)
            predicates.add(cb.equal(userRoot.get("city"),searchCriteria.getCity()));



        if (searchCriteria.getUserName() != null)
            predicates.add(cb.equal(userRoot.get("userName"),searchCriteria.getUserName()));



        if (searchCriteria.getEmail() != null)
            predicates.add(cb.equal(userRoot.get("email"),searchCriteria.getEmail()));


        if (searchCriteria.getAddress() != null)
            predicates.add(cb.equal(userRoot.get("address"),searchCriteria.getAddress()));




        if (searchCriteria.getPrimaryContact() != null)
            predicates.add(cb.equal(userRoot.get("userName"),searchCriteria.getPrimaryContact()));


        if (searchCriteria.getSecondaryContact() != null)
            predicates.add(cb.equal(userRoot.get("secondaryContact"),searchCriteria.getSecondaryContact()));





        criteriaQuery.where(cb.and(predicates.toArray(new Predicate[predicates.size()])));
        criteriaQuery.distinct(true);

        return entityManager.createQuery(criteriaQuery).getResultList();


    }
    @Override
    public List<Employee> search(SearchCriteria searchCriteria) {


        CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
        CriteriaQuery<Employee> criteriaQuery=criteriaBuilder.createQuery(Employee.class) ;

        Root<Employee> userRoot = criteriaQuery.from(Employee.class);

        List<Predicate> predicates = new ArrayList<Predicate>();



        for (final Map.Entry<String, String> e : searchCriteria.getFilters().entrySet())
        {

            final String key = e.getKey();
            final String value = e.getValue();

            if ((key != null) && (value != null)) {

                if (value.contains("%")) {
                    predicates.add(criteriaBuilder.like(userRoot.<String> get(key), value));
                } else {
                    predicates.add(criteriaBuilder.equal(userRoot.get(key), value));
                }
            }
        }

        criteriaQuery.where(criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()])));
        criteriaQuery.distinct(true);

        return entityManager.createQuery(criteriaQuery).setFirstResult(searchCriteria.getFirst()).setMaxResults(searchCriteria.getPageSize()).getResultList();


    }


}
